package siteflash.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import siteflash.data.SiteFlashPersistenceManage;
import siteflash.model.Game;

public class Sitemap extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws IOException {
		
		PersistenceManager psm = SiteFlashPersistenceManage.get().getPersistenceManager();
		//List all game
        Query query = psm.newQuery(Game.class);
        query.setFilter("status == 1");
        query.setOrdering("played desc");
        query.setRange(0, 50);
        List<Game> listResult = (List<Game>) query.execute();
        ArrayList<Game> games = new ArrayList<Game>();
        for (int i=0; i < listResult.size(); i++) {
            games.add(listResult.get(i));
        }
        
        req.setAttribute("games", games);
        try {
            req.getRequestDispatcher("/pages/sitemap.jsp").forward(req, resp);
        } catch (Exception e) {
            //TODO: handle exception
            e.printStackTrace();
        }
	}
}
